package baidu.reader.common.ui.toc
{
    import baidu.reader.common.*;
    import baidu.reader.common.model.*;
    import baidu.reader.common.model.VO.*;
    import baidu.ui.layout.*;
    import baidu.ui.managers.*;
    
    import flash.display.*;
    import flash.events.*;

    public class ToCContainer extends AutoLayoutContainer
    {
        private var _tocPage:ToCPage;
		[Embed(source="assets/sprite35.swf")]
		private var PageBG:Class;
        private var pageBG:Sprite;
		[Embed(source="assets/sprite62.swf")]
		private var LeftPageBG:Class;
        private var leftBG:Sprite;
		[Embed(source="assets/sprite59.swf")]
		private var RightPageBG:Class;
        private var rightBG:Sprite;
        private var prefModel:AppPreferenceModel;
        private var currentDisplayType:String;

        public function ToCContainer()
        {
            this.pageBG = new PageBG();
            this.leftBG = new LeftPageBG();
            this.rightBG = new RightPageBG();
            this.initModel();
            this.layoutUI();
            this.addEventListener(Event.ADDED_TO_STAGE, this.addToStageHandler);
            return;
        }

        private function initModel() : void
        {
            this.prefModel = InstanceManager.createSingletonInstance(AppPreferenceModel);
            this.currentDisplayType = this.prefModel.displayType;
            return;
        }

        private function displayChangeHandler(event:ReaderEvent) : void
        {
            if (this.prefModel.displayType != this.currentDisplayType)
            {
                this.layoutUI();
                this.currentDisplayType = this.prefModel.displayType;
            }
            return;
        }

        private function layoutUI() : void
        {
            this.addPageBG();
            this.addPageContent();
            return;
        }

        private function addToStageHandler(event:Event) : void
        {
            if (this.prefModel.displayType != this.currentDisplayType)
            {
                this.layoutUI();
                this.currentDisplayType = this.prefModel.displayType;
            }
            return;
        }

        private function addPageContent() : void
        {
            if (this._tocPage == null)
            {
                this._tocPage = new ToCPage();
            }
            this.removeDisplayObjects([this._tocPage]);
            addChild(this._tocPage);
            var _loc_1:* = this._childsLayoutInfo[this._tocPage];
            _loc_1.offsetCenterH = NaN;
            _loc_1.left = -40;
            _loc_1.right = 4;
            _loc_1.heightPercent = 100;
            this.drawNow();
            return;
        }

        private function addPageBG() : void
        {
            this.removeDisplayObjects([this.pageBG, this.leftBG, this.rightBG]);
            if (this.prefModel.displayType == AppPreferenceVO.DOUBLE_PAGE)
            {
                this.addSinglePageBG();
            }
            else
            {
                this.addSinglePageBG();
            }
            return;
        }

        private function removeDisplayObjects(param1:Array) : void
        {
            var _loc_3:DisplayObject = null;
            var _loc_2:int = 0;
            while (_loc_2 < param1.length)
            {
                
                _loc_3 = param1[_loc_2];
                if (_loc_3 && _loc_3.parent)
                {
                    _loc_3.parent.removeChild(_loc_3);
                }
                _loc_2++;
            }
            return;
        }

        private function addDoublePageBG() : void
        {
            addChild(this.leftBG);
            var _loc_1:* = this._childsLayoutInfo[this.leftBG];
            _loc_1.offsetCenterH = 0;
            _loc_1.widthPercent = 50;
            _loc_1.heightPercent = 100;
            addChild(this.rightBG);
            var _loc_2:* = this._childsLayoutInfo[this.rightBG];
            _loc_2.offsetCenterH = 0;
            _loc_2.widthPercent = 50;
            _loc_2.heightPercent = 100;
            return;
        }

        private function addSinglePageBG() : void
        {
            addChild(this.pageBG);
            var _loc_1:* = this._childsLayoutInfo[this.pageBG] as LayoutInfo;
            _loc_1.left = -20;
            _loc_1.right = 0;
            _loc_1.heightPercent = 100;
            return;
        }

        public function get tocPage() : ToCPage
        {
            return this._tocPage;
        }

    }
}
